<?php
/**
 * cloudtrip
 * ============================================================================
 * 版权所有 2016-2026 欣才开裆裤开发组
 * 网站地址: http://git.oschina.net/ts128
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 。
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 幸福的路痴
 * Date: 2016-10-15
 */
namespace Admin\Controller;
use Admin\Controller\BaseController;
class OrdersController extends BaseController{
    private $perpage = 10;
    private $M;
    public function _initialize(){
        $this->M = M("orders");
    }
    /*
    *   列表显示页面
    */
    public function index(){
        // 获取订单类型
        $type = I("type");
        if(empty($type)){
            $type = 0;
        }
        // 根据不同的订单类型设置查询条件
        switch($type){
            case 0:
                $condition = $this->hotels();
                break;
            case 1:
                $condition = $this->senic();
                break;
            case 2:
                $condition = $this->group();
                break;
            default:
                $this->error("订单列表页出错，请联系管理员", U("Orders/index"));
        }
        $key = I("key");
        // 查询数据并作分页处理
        $count = $this->M->count();    // 总记录数
        $pages = ceil($count / $this->perpage);
        $nowpage = I("p");
        if(empty($nowpage)){
            $nowpage = 1;
        }
        $Page = new \Think\Page($count, $this->perpage);
        $Page->setConfig("prev", "上一页");
        $Page->setConfig("next", "下一页");
        $this->M->alias("o")->join($condition["joins"])->field($condition["fields"])->where($condition["where"])->where("o.status != 2");
        if(!empty($key)){
            $this->M->where("o.oid like '%".$key."%' or u.nickname like '%".$key."%'");
        }
        $list = $this->M->limit($Page->firstRow.','.$Page->listRows)->select();   // 每页的数据数组
        $show = $Page->show();  // 页码链接内容
        $this->assign("count", $count);
        $this->assign("list", $list);
        $this->assign("page", $show);
        $this->assign("pages", $pages);
        $this->assign("nowpage", $nowpage);
        $this->assign("type", $type);
        session_start();
        $id=I("get.");
        $id=$id["id"];
        $_SESSION["pid"]=$id;
        $id=$_SESSION["pid"];
        $M=M("admin_node");
        $node=$M->where("parent_id='$id'")->select();
        $_SESSION["node"]=$node;
        $this->display();
    }
    /*
    *   酒店订单的查询条件
    */
    public function hotels(){
        $condition["fields"] = array(
            "o.id"=>"id",
            "oid",
            "status",
            "o.price"=>"price",
            "comment_times",
            "order_time",
            "o.type"=>"type",
            "nickname",
            "checkin",
            "checkout",
            "h.name"=>"hotel",
            "t.name"=>"roomtype"
        );
        $condition["joins"] = array(
            "LEFT JOIN __USERS__ u on u.id=o.users_id",
            "LEFT JOIN __ORDERS_ROOMS__ o_r on o_r.orders_id=o.id",
            "LEFT JOIN __ROOMS__ r on o_r.rooms_id=r.id",
            "LEFT JOIN __HOTELS__ h on r.hotels_id=h.id",
            "LEFT JOIN __ROOMS_TYPE__ t on r.type=t.id"
        );
        $condition["where"] = array(
            "o.type=0"
        );
        return $condition;
    }
    /*
    *   景点订单的查询条件
    */
    public function senic(){
        $condition["fields"] = array(
            "o.id"=>"id",
            "oid",
            "status",
            "price",
            "comment_times",
            "order_time",
            "type",
            "nickname",
            "adult_nums",
            "child_nums",
            "day",
            "name"=>"senic"
        );
        $condition["joins"] = array(
            "LEFT JOIN __USERS__ u on u.id=o.users_id",
            "LEFT JOIN __ORDERS_SENIC__ os on os.orders_id=o.id",
            "LEFT JOIN __SENIC__ s on s.id=os.senic_id"
        );
        $condition["where"] = array(
            "o.type=1"
        );
        return $condition;
    }
    /*
    *   套餐订单的查询条件
    */
    public function group(){
        $condition["fields"] = array(
            "o.id"=>"id",
            "oid",
            "status",
            "o.price",
            "comment_times",
            "order_time",
            "o.type",
            "nickname",
            "og.nums",
            "day",
            "rooms_nums",
            "h.name"=>"hotel",
            "t.name"=>"roomtype",
            "s.name"=>"senic"
        );
        $condition["joins"] = array(
            "LEFT JOIN ".C("DB_PREFIX")."users u on u.id=o.users_id",
            "LEFT JOIN __ORDERS_GROUP__ og on og.orders_id=o.id",
            "LEFT JOIN __GROUP__ g on og.group_id=g.id",
            "LEFT JOIN __ROOMS__ r on g.rooms_id=r.id",
            "LEFT JOIN __HOTELS__ h on r.hotels_id=h.id",
            "LEFT JOIN __ROOMS_TYPE__ t on r.type=t.id",
            "LEFT JOIN __SENIC__ s on s.id=g.senic_id"
        );
        $condition["where"] = array(
            "o.type=2"
        );
        return $condition;
    }
    /*
    *   订单编辑
    */
    public function edit(){
        // 获取操作的订单编号
        $id = I("id");
        if(empty($id)){
            $this->error("非法操作", U("Orders/index"));
        }
        // 获取订单类型
        $type = I("type");
        if(empty($type)){
            $type = 0;
        }
        // 根据不同的订单类型设置查询条件
        switch($type){
            case 0:
                $condition = $this->hotels();
                break;
            case 1:
                $condition = $this->senic();
                break;
            case 2:
                $condition = $this->group();
                break;
            default:
                $this->error("订单列表页出错，请联系管理员", U("Orders/index"));
        }
        $data = $this->M->alias("o")->join($condition["joins"])->field($condition["fields"])->where($condition["where"])->where("o.id=".$id)->select();
        $this->assign("data", $data[0]);
        $this->display();
    }
    /*
    *   修改订单处理程序
    */
    public function doEdit(){
        $id = I("id");
        if(empty($id)){
            $this->error("非法操作", U("Orders/index"));
        }
        // 获取订单类型
        $type = I("type");
        if(empty($type)){
            $type = 0;
        }
        $data[0] = $this->M->create();
        // 根据不同的订单类型修改订单
        $this->M->startTrans();
        $res1 = $this->M->where("type=".$type." and id=".$id)->save($data[0]);
        switch($type){
            case 0:
                $table = "orders_rooms";
                $fields = array("checkin","checkout");
                $data[1] = $this->M->table(C("DB_PREFIX").$table)->create();
                $data[1]["checkin"] = strtotime($data[1]["checkin"]);
                $data[1]["checkout"] = strtotime($data[1]["checkout"]);
                break;
            case 1:
                $table = "orders_senic";
                $fields = "day";
                $data[1] = $this->M->table(C("DB_PREFIX").$table)->create();
                $data[1]["day"] = strtotime($data[1]["day"]);
                break;
            case 2:
                $table = "orders_group";
                $fields = "";
                $data[1] = $this->M->table(C("DB_PREFIX").$table)->create();
                break;
            default:
                $this->error("订单列表页出错，请联系管理员", U("Orders/index"));
        }
        $res2 = $this->M->table(C("DB_PREFIX").$table)->where("orders_id=".$id)->field($fields)->filter('strip_tags')->save($data[1]);
        if($res1 || $res2){
            $this->M->commit();
            $this->success("修改成功", U("Orders/index")."&type=".$type);
        }
        else{
            $this->M->rollback();
            $this->success("修改失败", U("Orders/index")."&type=".$type);
        }
    }
    /*
    *   删除订单操作
    */
    public function del(){
        $id = I("id");
        if(empty($id)){
            $this->error("非法操作", U("Orders/index"));
        }
        $data["status"] = 2;
        $res = $this->M->where("id=".$id)->save($data);
        if($res){
            $this->success("删除成功", U("Orders/index"));
        }
        else{
            $this->success("删除失败", U("Orders/index"));
        }
    }
}